package com.Utility;

import java.util.regex.Pattern;

import com.objects.ValidateTextResponse;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.EditText;

public class ValidateText {
	/*
	 * public methods
	 */

	public ValidateTextResponse validateEmail(EditText edt, int limit) {
		String email = edt.getText().toString().trim();
		Pattern pattern = Pattern
				.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
		if (email.length() == 0) {
			// edt.setError(setColor("Please input data"));
			return new ValidateTextResponse(false,
					"You must enter a valid email format");
		} else if (email.length() > limit) {
			// edt.setError(setColor("The data must less than " + limit
			// + " characters"));
			return new ValidateTextResponse(false, "The email must less than "
					+ limit + " characters");
		} else if (!pattern.matcher(email).matches()) {
			// edt.setError(setColor("The email address must be xxx@xxx.xxx"));
			return new ValidateTextResponse(false,
					"You must enter a valid email format");
		}
		return new ValidateTextResponse(true, null);
	}

	public ValidateTextResponse validateTextLength(EditText edt, int minLimit,
			int maxLimit) {
		String text = edt.getText().toString().trim();
		if (text.length() < minLimit) {
			// edt.setError(setColor("The data at least " + minLimit
			// + " characters"));
			return new ValidateTextResponse(false, "The data at least "
					+ minLimit + " characters");
		} else if (text.length() > maxLimit) {
			// edt.setError(setColor("The data between " + minLimit + " and "
			// + maxLimit + " characters"));
			return new ValidateTextResponse(false, "The data between "
					+ minLimit + " and " + maxLimit + " characters");
		}
		return new ValidateTextResponse(true,null);
	}

	public SpannableStringBuilder setColor(String str) {
		ForegroundColorSpan fgcspan = new ForegroundColorSpan(0xff2fb0e0);
		SpannableStringBuilder ssbuilder = new SpannableStringBuilder(str);
		ssbuilder.setSpan(fgcspan, 0, str.length(), 0);
		return ssbuilder;
	}
}
